/**
  ******************************************************************************
  * @file           : ethernet.h
  * @brief          : Header for ethernet.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __ETHERNET_H
#define __ETHERNET_H

#include "socket.h"
#include "tskcfg.h"

#define ETH_INFO_LOG
#define ETH_DEBUG_LOG

#define CQUE     0
#define DQUE     1

#define ETHERNET_CTRL_QUE_LENGTH         8
#define ETHERNET_CTRL_QUE_ITEMSIZE       sizeof(Ethernet_CtrlQueStru)

#define ETHERNET_SEND_QUE_LENGTH         8
#define ETHERNET_SEND_QUE_ITEMSIZE       sizeof(Ethernet_SendQueStru)

#define ETHERNET_ISR_QUE_LENGTH          8
#define ETHERNET_ISR_QUE_ITEMSIZE        sizeof(uint8_t)

/* 缓存大小会导致消息队列占用的RAM成倍增加,且memcpy开销也会增加 */
#define ETHERNET_TXBUF_MAXLEN            512
#define ETHERNET_RXBUF_MAXLEN            512

/* 预分频socket号和本地端口,若需要动态,再做程序适配 */
#define ETH_SOCK_NUM_PRODUP           0
#define ETH_SOCK_NUM_PRODRT           1
#define ETH_SOCK_NUM_MBTCP            2
#define ETH_SOCK_LCPORT_PRODUP        55000
#define ETH_SOCK_LCPORT_PRODRT        55001
#define ETH_SOCK_LCPORT_MBTCP         502

#define ETH_SOCK_0_FBQHD              produp_ethfb_qhd
#define ETH_SOCK_1_FBQHD              prodrt_ethfb_qhd
#define ETH_SOCK_2_FBQHD              mbtcp_ethfb_qhd
#define ETH_SOCK_3_FBQHD              NULL

/* 控制消息:socket初始化 socket关闭 socket监听 socket连接 socket断开 等 */
typedef enum _Ethernet_CtrlTypeEnum
{
  ETH_CRTL_SOCKET,
  ETH_CRTL_CLOSE,
  ETH_CRTL_LISTEN,
  ETH_CRTL_CONNECT,
  ETH_CRTL_DISCONNECT,
  ETH_CRTL_GETSOCKETSR,
}Ethernet_CtrlTypeEnum;

typedef struct _Ethernet_CtrlQueStru
{
  Ethernet_CtrlTypeEnum type;
  uint8_t  sn;
  uint8_t  protocol;
  uint8_t  flag;
  uint8_t  dest_ip[4];            /* 目标地址 */
  uint16_t dest_port;             /* 目标端口 */
  uint16_t local_port;            /* 本地端口 */
}Ethernet_CtrlQueStru;

/* 发送消息:TCP发送 其他(UDP)发送 */
typedef struct _Ethernet_SendQueStru
{
  uint8_t  sn;
  uint8_t  type;
  uint8_t  dummy1;
  uint8_t  dummy2;
  uint8_t  dest_ip[4];
  uint16_t dest_port;
  uint16_t len;
  uint8_t  buf[ETHERNET_TXBUF_MAXLEN];
}Ethernet_SendQueStru;

/* 返回消息结构体 */
typedef enum _Ethernet_FeedbackTypeEnum
{
  ETH_FB_CONNECTED,
  ETH_FB_DISCONNECTED,
  ETH_FB_TIMEOUT,
  ETH_FB_SENTOK,
  ETH_FB_SOCKETSR,
}Ethernet_FeedbackTypeEnum;

typedef struct _Ethernet_FeedbackCtrlStru
{
  uint8_t type;
  uint8_t sr;
}Ethernet_FeedbackCtrlStru;

typedef struct _Ethernet_FeedbackDataStru
{
  uint16_t len;
  uint16_t sec_port;
  uint8_t  src_ip[4];
  uint8_t  buf[ETHERNET_RXBUF_MAXLEN];
}Ethernet_FeedbackDataStru;

extern QueueHandle_t *sn2fbq(uint8_t sn);

extern void ethos_wizchip_initialize(void);
extern void ethos_wizchip_getinterrupt(void);
extern void ethos_ctrl_socket(const Ethernet_CtrlQueStru *ctrl);
extern void ethos_ctrl_close(const Ethernet_CtrlQueStru *ctrl);
extern void ethos_ctrl_listen(const Ethernet_CtrlQueStru *ctrl);
extern void ethos_ctrl_connect(const Ethernet_CtrlQueStru *ctrl);
extern void ethos_ctrl_disconnect(const Ethernet_CtrlQueStru *ctrl);
extern void ethos_ctrl_getsocketsr(const Ethernet_CtrlQueStru *ctrl);
extern void ethos_send(const Ethernet_SendQueStru *tosend);
extern void ethos_sendto(const Ethernet_SendQueStru *tosend);

extern void ethif_socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag);
extern void ethif_close(uint8_t sn);
extern void ethif_listen(uint8_t sn);
extern void ethif_connect(uint8_t sn, const uint8_t *addr, uint16_t port);
extern void ethif_disconnect(uint8_t sn);
extern uint8_t ethif_get_socket_sr(uint8_t sn);   /* 最大会阻塞127ms */
extern void ethif_tosend(const Ethernet_SendQueStru *tosend);

extern volatile uint8_t ethif_get_sr[_WIZCHIP_SOCK_NUM_];

/* ethernet debug macro */
#ifdef ETH_INFO_LOG
  #define  EthInfLog(...)     _printf("[ETH INF]:");_printf(__VA_ARGS__);
#else
  #define EthInfLog(...)
#endif

#ifdef ETH_DEBUG_LOG
  #define  EthDbgLog(...)      _printf("[ETH DBG]:");_printf(__VA_ARGS__);                           
#else
  #define EthDbgLog(...)
#endif

#ifdef ETH_ERROR_LOG
  #define  EthErrLog(...)      _printf("[ETH ERR]:");_printf(__VA_ARGS__);
#else
  #define EthErrLog(...)
#endif

#endif /* __ETHERNET_H */
